<template>
  <div class="login-container flx-center" v-if="loading" v-loading="loading"></div>
  <div class="login-container flx-center" v-else>
    <div class="login-box">
      <SwitchDark class="dark" />
      <div class="login-left">
        <img class="login-left-img" src="@/assets/images/login_left.png" alt="login" />
      </div>
      <div class="login-form">
        <div class="login-logo">
          <!-- <img class="login-icon" src="@/assets/images/logo.png" alt="" /> -->
          <h4 class="logo-text">{{ _transitionLanguages('login', '用户登录') }}</h4>
        </div>
        <LoginForm />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="login">
import LoginForm from './components/LoginForm.vue';
import SwitchDark from '@/components/SwitchDark/index.vue';
import { initDynamicRouter } from '@/routers/modules/dynamicRouter';
const tabsStore = useTabsStore();
const keepAliveStore = useKeepAliveStore();

onBeforeMount(() => {
  isLogin();
});

// 加载状态
let loading = ref(false);

// 是否直接登录
const isLogin = async () => {
  const userStore = useUserStore();
  const route = useRoute();
  const params = route?.query || {};

  const { origin, token, redirect, code } = params;

  // 从App端跳转
  if (origin === 'APP') {
    loading.value = true;
    let userObj: any = {};
    Object.keys(params).forEach(key => {
      if (key !== 'origin' && key !== 'token') {
        userObj[key] = params[key];
      }
    });
    userStore.setToken(token as string);
    userStore.setUserInfo(userObj);
    setTimeout(() => {
      routerPush('/');
    }, 100);
  }

  if (redirect) {
    loading.value = true;
    window.location.href = 'http://mes.cnrecyc.com/api/auth/wxAuthMesPc/login';

    localSet('redirect_url', redirect);
  }
  // 从企微获取到code码
  if (code) {
    // loading.value = true;
    // const data = await wxAuthGetUser({ code: location?.href });
    // const { token, userObj } = data;
    // userStore.setToken(token);
    // userStore.setUserInfo(userObj);
    // // 2.添加动态路由
    // await initDynamicRouter();
    // // 3.清空 tabs、keepAlive 数据
    // tabsStore.setTabs([]);
    // keepAliveStore.setKeepAliveName([]);
    // const redirect_url = localGet('redirect_url');
    // localRemove('redirect_url');
    // // 4.跳转到首页
    // routerPush(redirect_url);
  }
};

// 语言转换
const _transitionLanguages = (v: string, l: string): string => {
  return transitionLanguages(v, l);
};
</script>

<style scoped lang="scss">
@import './index.scss';
</style>
